import { Component, Vue } from 'vue-property-decorator'

@Component
export default class BaseCurd extends Vue {
  public tableDataLoading = false
  public tableDataTotal = 0
  public tableData: any = []
  public tableDataStatus: any = {}

  mounted() {
    this.getTableData()
  }

  onReset(key: string) {
    console.log('onReset', key)
  }

  onPaginationChange() {
    this.getTableData()
  }

  onFilter() {
    // 筛选列表是重新初始化分页参数
    (this.$refs.table as any).resetPaginationParams()
    console.log('onFilter')
    this.getTableData()
  }

  onSearch() {
    console.log('onSearch')
    this.getTableData()
  }

  // 进行筛选的总的请求，通过searchParams对象的参数，进行有条件的筛选
  async getTableData() {
    try {
      console.log('getTableData')
      this.tableDataLoading = true
      const searchParams = this.getSearchParams()
      await this.fetchTableData(searchParams)
    } finally {
      this.tableDataLoading = false
    }
  }

  // 获取筛选的全部条件
  getSearchParams(): any {
    const searchParams =
      (this.$refs.search &&
        (this.$refs.search as any).getSearchParamsValues()) ||
      {}
    const paginationParams = (this.$refs.table as any).getPaginationParams()
    return { ...searchParams, ...paginationParams }
  }

  fetchTableData(searchParams: any) {
    console.log('super fetchTableData', searchParams)
  }
}
